//
//  CAOrderDetailGoodsView.m
//  LNH
//
//  Created by twentylx on 2018/2/14.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CAOrderDetailGoodsView.h"

@implementation CAOrderDetailGoodsView

/**
 为视图绑定 viewModel
 
 @param viewModel 要绑定的ViewModel
 @param params 额外参数
 */
- (void)bindViewModel:(id <LMViewProtocol>)viewModel withParams:(NSDictionary *)params {
    
}

/**
 初始化额外数据
 */
- (void)lm_initializeForView {
    self.backgroundColor = [UIColor whiteColor];
}

/**
 初始化视图
 */
- (void)lm_createViewForView {
    [self setNeedsUpdateConstraints];
    
    [self addSubview:self.goodsTitleLabel];
    [self addSubview:self.totalLabel];
    [self addSubview:self.freightLabel];
    [self addSubview:self.lastView];
}

- (void)updateConstraints {
    [super updateConstraints];
    
    [_goodsTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(20);
        make.top.equalTo(self).offset(10);
    }];
    
//    for (UIView *view in [self subviews]) {
//        [view removeFromSuperview];
//    }
    __block UIView *lastView = nil;
    
    
    [self.goodsArray enumerateObjectsUsingBlock:^(NSDictionary *goodsModel, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIView *goodsBackgroundView = [[UIView alloc] init];
        goodsBackgroundView.backgroundColor = [UIColor whiteColor];
        [self addSubview:goodsBackgroundView];
        
        UIImageView *goodsImageView = [UIImageView new];
        [self addSubview:goodsImageView];
        [goodsImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", BASE_IMAGE_DOMAIN, goodsModel[@"goods_img"]]]];
        goodsImageView.layer.borderWidth = 1;
        
        UILabel *goodsNameLabel = [UILabel new];
        goodsNameLabel.numberOfLines = 3;
        goodsNameLabel.lineBreakMode = NSLineBreakByCharWrapping;
        goodsNameLabel.font = [UIFont systemFontOfSize:16];
        [self addSubview:goodsNameLabel];
        goodsNameLabel.text = goodsModel[@"goods_name"];
        
        UILabel *brandLabel = [UILabel new];
        [self addSubview:brandLabel];
        brandLabel.textColor = RGBCOLOR(116, 116, 116);
        brandLabel.font = [UIFont systemFontOfSize:14];
        brandLabel.numberOfLines = 2;
        brandLabel.lineBreakMode = NSLineBreakByCharWrapping;
        
        NSArray *attrArray = goodsModel[@"attr"];
        __block NSString *attrStr = @"";
        [attrArray enumerateObjectsUsingBlock:^(NSDictionary *attrDict, NSUInteger idx, BOOL * _Nonnull stop) {
            attrStr = [NSString stringWithFormat:@"%@ %@:%@", attrStr, attrDict[@"name"], attrDict[@"attr_value"]];
        }];
        brandLabel.text = attrStr;
        
        UILabel *priceLabel = [UILabel new];
        [self addSubview:priceLabel];
        priceLabel.textColor = RGBCOLOR(233, 85, 19);
        priceLabel.font = [UIFont systemFontOfSize:14];
        priceLabel.textAlignment = NSTextAlignmentRight;
        priceLabel.text = [NSString stringWithFormat:@"￥%@", goodsModel[@"goods_price"]];
        
        
        UIButton *returnButton = [UIButton new];
        [self addSubview:returnButton];
        [returnButton setTitle:@"售后" forState:UIControlStateNormal];
        [returnButton setTitleColor:RGBCOLOR(62, 62, 62) forState:UIControlStateNormal];
        returnButton.titleLabel.font = [UIFont systemFontOfSize:16];
        returnButton.layer.borderWidth = 1;
        returnButton.layer.borderColor = [RGBCOLOR(157, 157, 157) CGColor];
        returnButton.layer.cornerRadius = 5;
        if (self.isUpgradeOrder == 1) {
            returnButton.hidden = YES;
        }
        
        UIView *lineView = [UIView new];
        [self addSubview:lineView];
        lineView.backgroundColor = RGBCOLOR(224, 224, 224);
        
        
        [goodsBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.equalTo(self);
            if (idx == 0) {
                make.top.equalTo(self).offset(30);
            } else {
                make.top.equalTo(lastView.mas_bottom);
            }
            make.height.mas_equalTo(120);
        }];
        
        [goodsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(goodsBackgroundView).offset(10);
            make.top.equalTo(goodsBackgroundView).offset(10);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(100);
        }];
        
        [goodsNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(goodsImageView.mas_right).offset(10);
            make.top.equalTo(goodsImageView).offset(5);
            make.right.equalTo(self).offset(-80);
        }];
        
        [brandLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(goodsNameLabel);
            make.top.equalTo(goodsNameLabel.mas_bottom).offset(10);
            make.right.equalTo(self).offset(-80);
        }];
        
        [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(goodsNameLabel);
            make.top.equalTo(brandLabel.mas_bottom).offset(10);
        }];
        
        [returnButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-20);
            make.bottom.equalTo(goodsImageView);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(20);
        }];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.equalTo(goodsBackgroundView);
            make.bottom.equalTo(goodsBackgroundView).offset(10);
            make.height.mas_equalTo(1);
        }];
        
        lastView = goodsBackgroundView;
        self.lastView = lastView;
    }];
    
    [_totalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(20);
        make.top.equalTo(self.lastView.mas_bottom).offset(20);
    }];
    
    [_freightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.totalLabel);
        make.top.equalTo(self.totalLabel.mas_bottom).offset(10);
    }];
}

#pragma mark get & set
- (UILabel *)goodsTitleLabel {
    if (!_goodsTitleLabel) {
        _goodsTitleLabel = [UILabel new];
        _goodsTitleLabel.text = @"商品信息";
        _goodsTitleLabel.textColor = RGBCOLOR(62, 58, 57);
        _goodsTitleLabel.font = [UIFont systemFontOfSize:16];
    }
    return _goodsTitleLabel;
}

- (UILabel *)totalLabel {
    if (!_totalLabel) {
        _totalLabel = [UILabel new];
        _totalLabel.text = @"商品价格：";
        _totalLabel.textColor = RGBCOLOR(62, 58, 57);
        _totalLabel.font = [UIFont systemFontOfSize:16];
    }
    return _totalLabel;
}

- (UILabel *)freightLabel {
    if (!_freightLabel) {
        _freightLabel = [UILabel new];
        _freightLabel.text = @"配送费：";
        _freightLabel.textColor = RGBCOLOR(62, 58, 57);
        _freightLabel.font = [UIFont systemFontOfSize:16];
    }
    return _freightLabel;
}

@end
